package com.sgb.kjwl.view.ui.adapter.packers;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.packers.OnLineEntity;

import java.util.List;
import java.util.Locale;

/**
 * 联系人adapter
 */
public class LinkPeopleAdapter extends BaseExpandableListAdapter {
    private List<OnLineEntity.ChildBean> departmentEntityList;
    private Context context;

    public LinkPeopleAdapter(Context context, List<OnLineEntity.ChildBean> departmentEntityList) {
        this.context = context;
        this.departmentEntityList = departmentEntityList;
    }

    @Override
    public int getGroupCount() {
        return departmentEntityList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return departmentEntityList.get(i).getOnlineConcat().size();
    }

    @Override
    public Object getGroup(int i) {
        return departmentEntityList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return departmentEntityList.get(i).getOnlineConcat().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    /**
     * 获取显示指定组的视图对象
     *
     * @param groupPosition 组位置
     * @param isExpanded    该组是展开状态还是伸缩状态，true=展开
     * @param convertView   重用已有的视图对象
     * @param parent        返回的视图对象始终依附于的视图组
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_company_departments, parent, false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvDepartment.setText(departmentEntityList.get(groupPosition).getDepartmentName());
        groupViewHolder.tvPeopleNum.setText(String.format(Locale.CHINA,"(%d人)",departmentEntityList.get(groupPosition).getSize()));
        //如果是展开状态，
        if (isExpanded) {
            groupViewHolder.imgExpand.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.btn_expand_up_gray));
        } else {
            groupViewHolder.imgExpand.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.btn_expand1));

        }

        if (groupPosition == departmentEntityList.size() - 1){
            groupViewHolder.vLine.setVisibility(View.GONE);
        }else {
            groupViewHolder.vLine.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_company_departments_member, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.tvMember.setText(departmentEntityList.get(groupPosition).getOnlineConcat().get(childPosition).getName());

        childViewHolder.ivSelect.setSelected(departmentEntityList.get(groupPosition).getOnlineConcat().get(childPosition).isSelect());
        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupViewHolder {
        TextView tvDepartment;
        TextView tvPeopleNum;
        ImageView imgExpand;
        View vLine;

        public GroupViewHolder(View convertView) {
            tvDepartment = convertView.findViewById(R.id.tv_department);
            tvPeopleNum = convertView.findViewById(R.id.tv_people_num);
            imgExpand = convertView.findViewById(R.id.img_expand);
            vLine = convertView.findViewById(R.id.v_line);
        }

    }

    /**
     * 提供给Activity刷新数据
     *
     * @param list
     */
    public void updateList(List<OnLineEntity.ChildBean> list) {
        this.departmentEntityList = list;
        notifyDataSetChanged();
    }

    private class ChildViewHolder {
        TextView tvMember;
        ImageView ivSelect;

        public ChildViewHolder(View convertView) {
            tvMember = convertView.findViewById(R.id.tv_member);
            ivSelect =convertView.findViewById(R.id.iv_select);
        }
    }
}